package com.blog.config;




import com.blog.authentication.MyShiroRealm;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
public class ShiroConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ShiroFilterFactoryBean shirFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 必须设置 SecurityManager
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 拦截器. 按顺序编写
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        // 设置login URL
        shiroFilterFactoryBean.setLoginUrl("/login.html");
        // src="jquery/jquery-3.2.1.min.js" 生效
        filterChainDefinitionMap.put("/loginController/**", "anon");
        // 设置登录的URL为匿名访问，因为一开始没有用户验证
        filterChainDefinitionMap.put("/admin/**", "anon");
        filterChainDefinitionMap.put("/page/login/**", "anon");
        //前端页面静态不拦截
        filterChainDefinitionMap.put("/static/**", "anon");
        // 不拦截登录注册模块接口路径
        filterChainDefinitionMap.put("/loginController/*", "anon");
        // 不拦截vue模块博客
        filterChainDefinitionMap.put("/api/**", "anon");
        // 图片不拦截
        filterChainDefinitionMap.put("/file/upload/**", "anon");
        //上传图片模块不拦截
        filterChainDefinitionMap.put("/imgUpload/*", "anon");
        // 退出系统的过滤器
        filterChainDefinitionMap.put("/logout", "logout");
        // 最后一班都，固定格式
        filterChainDefinitionMap.put("/**", "authc");
        //将所以需要拦截的条件放入
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    @Bean
    public MyShiroRealm myShiroRealm() {
        //生成自定义权限对象
        MyShiroRealm myShiroRealm = new MyShiroRealm();
        return myShiroRealm;
    }

    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 注入自定义的realm;
        securityManager.setRealm(myShiroRealm());
        return securityManager;
    }

    /**
     *  设置session过期时间
     * @return
     */
    @Bean
    public DefaultWebSessionManager getWebSessionManager() {
        DefaultWebSessionManager defaultWebSessionManager = new DefaultWebSessionManager();
        defaultWebSessionManager.setGlobalSessionTimeout(10000L);
        return defaultWebSessionManager;
    }

    //配置mybatis的分页插件pageHelper
    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("dialect", "mysql");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }

    // 资源文件模块
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/path/**").addResourceLocations("file://img/");
        super.addResourceHandlers(registry);
    }

    // 配置全局跨域
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/api/**")
                        .allowedOrigins("http://localhost:8080")
                        .allowedMethods("PUT","POST","GET", "DELETE")
                        .allowedHeaders("header1", "header2", "header3")
                        .exposedHeaders("header1", "header2")
                        .allowCredentials(false).maxAge(3600);
            }
        };
    }

    /**
     *  设置文件上传大小
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大 50mb
        factory.setMaxFileSize(DataSize.ofKilobytes(1024*1024*50));
        //设置总上传数据大小
        factory.setMaxRequestSize(DataSize.ofKilobytes(1024*1024*50));
        return factory.createMultipartConfig();
    }

}
